﻿Imports WebSitePanel.SkinDesigner.Business
Public Class ucBrowser
    Public Event BrowserLoaded(ByVal Sender As Object, ByVal e As EventArgs)
    Private _url As String
    Public Sub New()
        InitializeComponent()
        Me.wb.Url = New Uri(My.Settings.WSP_URL)
        _url = My.Settings.WSP_URL
    End Sub
    Private Function GetStyleSheetObject(ByVal Name As String) As Object
        If Not Name.ToLower.EndsWith(".css") Then
            Name &= ".css"
        End If
        Dim texts As New List(Of Object)
        For Each item As Object In Me.wb.Document.DomDocument.StyleSheets
            Try
                If CStr(item.href).ToLower.Contains(Name.ToLower) Then
                    texts.Add(item)
                End If
            Catch ex As Exception
            End Try
        Next
        If texts IsNot Nothing AndAlso texts.Count > 0 Then
            Return texts(0)
        Else
            Return Nothing
        End If
    End Function
    Public Sub ApplyCss(ByVal Name As String, ByVal CssText As String)
        Dim css As Object = GetStyleSheetObject(Name)
        If css IsNot Nothing Then
            css.cssText = CssText
        End If
    End Sub
    Public ReadOnly Property DefaultUrl As String
        Get
            Return _url
        End Get
    End Property
    Public Sub UpdateImages()
        _images = New List(Of String)
        For Each el As Object In wb.Document.DomDocument.Images
            Dim source As String = el.src
            If (Not source.StartsWith("http://") OrElse source.StartsWith(DefaultUrl)) AndAlso Not source.Contains(".ashx") Then
                source = source.Replace(DefaultUrl, "file:///" & Toolbox.TempPath & "TemporaryFiles/")
                source = source.Replace("/Default/", "/Theme/")
                source = source.Replace("\", "/")
                source = source.Replace("\\", "\")
                el.src = source
                _images.Add(source.Replace("file:///", "").Replace("/", "\").Replace("\\", "\"))
            End If
        Next
    End Sub
    Public Sub ReloadImages()
        _images = New List(Of String)
        For Each el As Object In wb.Document.DomDocument.Images
            Dim src As String = el.src.ToString
            If src.IndexOf("guid") <> -1 Then
                src = src.Remove(src.IndexOf("?guid"))
            End If
            el.src = src & "?guid=" & Guid.NewGuid.ToString
        Next
    End Sub
    Private _images As List(Of String)
    Public ReadOnly Property Images As List(Of String)
        Get
            Return _images
        End Get
    End Property
    Private Sub wbExample_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted
        UpdateImages()
        Dim el = wb.Document.GetElementById("ctl02_ctl01_ctl00_txtUsername")
        If el IsNot Nothing Then
            wb.Document.GetElementById("ctl02_ctl01_ctl00_txtUsername").SetAttribute("value", My.Settings.WSP_Username)
            wb.Document.GetElementById("ctl02_ctl01_ctl00_txtPassword").SetAttribute("value", My.Settings.WSP_Password)
        End If
        RaiseEvent BrowserLoaded(Me, EventArgs.Empty)
    End Sub
    Public Sub Renavigate()
        wb.Navigate(wb.Url.ToString)
    End Sub

    Private Sub wb_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles wb.ProgressChanged
        Me.tsbBrowser.Visible = True
        If e.MaximumProgress > 0L AndAlso e.CurrentProgress > 0L Then
            Dim value As Integer = CInt(Math.Round((100 * e.CurrentProgress / e.MaximumProgress)))
            If value <= Me.tsbBrowser.Maximum AndAlso value >= Me.tsbBrowser.Minimum Then
                Me.tsbBrowser.Value = value
            End If
        ElseIf e.MaximumProgress = 0L AndAlso e.CurrentProgress = 0L Then
            Me.tsbBrowser.Visible = False
            Me.tsbBrowser.Value = 0
        End If
    End Sub

    Private Sub wb_StatusTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles wb.StatusTextChanged
        Me.tsslStatus.Text = wb.StatusText
    End Sub
    Public Property StatusBarVisible As Boolean
        Get
            Return Me.ssBrowser.Visible
        End Get
        Set(ByVal value As Boolean)
            Me.ssBrowser.Visible = value
        End Set
    End Property
End Class